<?php
class color {
/**
*   @desc Variable Declaration with default value
*/
	private $_id;   // KEY ATTR. WITH AUTOINCREMENT
     private $_pid;  
     private $_color;  
     
/**
*   @desc   CONSTRUCTOR METHOD
*/
	function __construct() {
		global $DBOBJ;
		$this->_obj = $DBOBJ;
		$this->_id = null; 
		$this->_pid = null; 
		$this->_color = null; 
	}

/**
*   @desc   DECONSTRUCTOR METHOD
*/
	function __destruct() {
		unset($this->_obj);
	}

/**
*   @desc   GETTER METHODS
*/

     public function getid() {
     	return $this->_id;
     }
     
     public function getpid() {
     	return $this->_pid;
     }
     
     public function getcolor() {
     	return $this->_color;
     }
     
/**
*   @desc   SETTER METHODS
*/

     public function setid($id) {
     	 $this->_id =  $id;
     }
     
     public function setpid($pid) {
     	 $this->_pid =  $pid;
     }
     
     public function setcolor($color) {
     	 $this->_color =  $color;
     }
     
/**
*   @desc   SELECT METHOD / LOAD
*/

	public function select($id) {
		 $sql =  "SELECT * FROM `color` WHERE `id` = '". $id ."' ";
		 $row =  $this->_obj->select($sql); 

		 if (count($row) > 0) {
			 $this->_id = $row[0]['id'];
     		 $this->_pid = $row[0]['pid'];
     		 $this->_color = $row[0]['color'];
     			 return $row[0];
		}else {
			 return;
		}
	}
/**
*   @desc   set All
*/
	 public function setAll($dataArray = array()) {
		 if (count($dataArray) <= 0) {
			return;
		}

		foreach ($dataArray as $key => $value) {
			$classKey = '_' . $key;
			$this->$classKey = $value;
		}
	}

/**
*   @desc   get All
*/

	 public function getAll($FORMAT = 'ARRAY') {
		$dataArray = new stdClass();
		$dataArray->id = $this->getid();
     	$dataArray->pid = $this->getpid();
     	$dataArray->color = $this->getcolor();
     	if ($FORMAT == 'ARRAY') {
			return (array) $dataArray;
		}
		return $dataArray;
	}
/**
*   @desc   COUNT
*/

	public function getCountcolor() {
		 $sql = "SELECT count(*) as cnt  FROM `color`";
		 $result = $this->_obj->select($sql);
		 return $result['0']['cnt'];
	}

/**
*   @desc   DELETE
*/
	public function delete($id) {
		 $sql = "DELETE FROM `color` WHERE `pid` = '". $id ."'";
		 $result = $this->_obj->sql_query($sql);
	}

/**
*   @desc   INSERT
*/
	public function insert() {
		 $this->id = ""; // clear key for autoincrement
		 $sql = "INSERT INTO `color` ( `id`,`pid`,`color` ) VALUES ( NULL,'".$this->_pid."','".$this->_color."' )";
		 $result = $this->_obj->insert($sql);
		 return $result;
	}

/**
*   @desc   UPDATE
*/
	public function update($id) {
		 $sql = " UPDATE `color` SET  `id` = '".$this->_id."' , `pid` = '".$this->_pid."' , `color` = '".$this->_color."'  WHERE `id` =   '".$id."' ";
		 $result = $this->_obj->sql_query($sql);
	}
	public function getAllColorList($params = "*", $where = "", $orderby = "id", $limit = "") {
		$orderby = "ORDER BY " . $orderby;
		$sql = "SELECT " . $params . " FROM `color` a  " . $where . " " . $orderby . " " . $limit;
		$row = $this->_obj->select($sql);
		if ($row) {
			return $row;
		}
		return array();
	}
	
	public function performListAction($action = "", $ids = "''") {
		switch ($action) {
			case "delete":
				$sql = "DELETE FROM `color` WHERE `id` IN (" . $ids . ")";
			break;
			default:
				$sql = "";
		}
		if ($sql == "") {
			return false;
		}
		$result = $this->_obj->sql_query($sql);
		return true;
    }
	
	
	public function getColorByProductId( $productId = '' ) {
		if ($productId == '') {
			return Array();
		}
		
		$sql = "SELECT * FROM `color` where pid = '".$productId."'";
		$result = $this->_obj->select($sql);
		$finalArray = Array();
		foreach($result as $color) {
			$finalArray[$color['id']] = $color['color'];
		}
		return $finalArray;
	}
/*

CREATE TABLE `color` (
`id` INT( 11 ) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`pid` INT( 11 ) UNSIGNED NOT NULL ,
`color` VARCHAR( 50 ) NOT NULL ,
INDEX ( `pid` )
) ENGINE = InnoDB;



*/
	
	

} 
?>